package com.zekdot.registerclient.dao;

import com.zekdot.registerclient.entity.EnrollTempletEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Create by zekdot on 20-4-7.
 */
@Mapper
@Repository
public interface EnrollTempletDao {

    /**
     * 插入一个模板
     * @param enrollTempletEntity 模板实体
     * @return
     */
    int save(EnrollTempletEntity enrollTempletEntity);

    /**
     * 根据id获取模板
     * @param id 模板id
     * @return
     */
    @Select("SELECT * FROM enroll_templet WHERE id=#{id} AND (status=1 || (status=0 AND author=#{userId}))")
    EnrollTempletEntity get(@Param("userId") String userId,@Param("id") String id);

    /**
     * 删除模板
     * @param id 模板id
     * @return
     */
    @Delete("DELETE FROM enroll_templet WHERE id=#{id} AND author=#{userId}")
    int delete(@Param("userId")String userId,@Param("id") String id);

    /**
     * 根据模板题目搜索模板
     * @param title 模板题目
     * @return
     */
    @Select("SELECT id,title,intro,author,sub_time,status FROM enroll_templet WHERE id>#{lastId} AND (status=1 OR (status=0 AND author=#{userId})) AND title LIKE concat(concat('%',#{title}),'%') LIMIT #{length}")
    List<EnrollTempletEntity> search(@Param("userId") String userId,@Param("title") String title,@Param("lastId") String lastId,@Param("length") int length);

    /**
     * 获取某用户制作的全部模板
     * @param userId 用户id
     * @param lastId 最后一条的id
     * @param length 长度
     * @return
     */
    @Select("SELECT id,title,intro,author,sub_time,status FROM enroll_templet WHERE id>#{lastId} AND author=#{userId} LIMIT #{length}")
    List<EnrollTempletEntity> selfTemplet(@Param("userId") String userId,@Param("lastId") String lastId,@Param("length") int length);

}
